package com.moon;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.util.PreferencesUtils;
import com.widget.CircleImageView;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ChildrenActivity extends BaseActivity {
	MyAdapter mAdapter;
	JSONArray mArrayList = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_classmate);
		
		mArrayList = JSON.parseArray(PreferencesUtils.getString(this, "classes"));
		
		((TextView) findViewById(R.id.center)).setText("班级列表");
		findViewById(R.id.left).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		final PullToRefreshListView ptrlvHeadlineNews = (PullToRefreshListView) findViewById(R.id.listview);
		ptrlvHeadlineNews.setMode(com.handmark.pulltorefresh.library.PullToRefreshBase.Mode.DISABLED);
		ListView listview = ptrlvHeadlineNews.getRefreshableView();
		listview.setVerticalScrollBarEnabled(false);
		listview.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				JSONObject item = mArrayList.getJSONObject(arg2 - 1);
				String name = item.getString("class_alias");
				if(name == null || name.length() == 0)
				{
					name = item.getString("school_name") + item.getString("class_name");
				}
				
				Intent intent = new Intent(ChildrenActivity.this,CourseActivity.class);
				intent.putExtra("id", item.getString("id"));
				intent.putExtra("name", name);
				startActivity(intent);
			}
		});
		mAdapter = new MyAdapter();
		listview.setAdapter(mAdapter);
	}
	
	@SuppressLint("InflateParams")
	class MyAdapter extends BaseAdapter{
		public int getCount() {
			if(mArrayList == null) return 0;
			return mArrayList.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			FoodHolder foodHold = null;

			if (convertView == null) {
				LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				foodHold = new FoodHolder();

				convertView = mInflater.inflate(R.layout.item_children, null);
				
				foodHold.icon = (CircleImageView)convertView.findViewById(R.id.icon);
				foodHold.text = (TextView)convertView.findViewById(R.id.text);
				convertView.setTag(foodHold);
			} else {
				foodHold = (FoodHolder) convertView.getTag();
			}

			JSONObject item = mArrayList.getJSONObject(position);
			String name = item.getString("class_alias");
			if(name == null || name.length() == 0)
			{
				name = item.getString("school_name") + item.getString("class_name");
			}
			else
			{
				name = item.getString("school_name") + name;
			}
			foodHold.text.setText(name);
			return convertView;
		}

		public final class FoodHolder {
			public CircleImageView icon;
			public TextView text;
		}
	}
}
